﻿using Common.Attributes;
using MODEL;
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.Zmjh
{
    public class ZmApplyController :Controller
    {

        #region public ActionResult index()+视图
        /// <summary>
        /// index视图
        /// </summary>
        /// <returns></returns>
        public ActionResult index()
        {
            return View();
        } 
        #endregion

        #region 获取竹铭课程报名学生数据列表 + GetZmApplyData()   +方法1、实现 多表显示在一个视图*****
        [HttpPost]
        [AjaxRequest]
        /// <summary>
        /// 获取竹铭课程报名学生数据列表
        /// </summary>
        /// <retur
        public ActionResult GetZmApplyData()
        {
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);

            //SetPeriodDropDownList();

            var list = OperateContext.Current.BLLSession.Iqgzx_zm_applyBLL.GetPagedList(pageIndex, pageSize, x => x.ApplyId != null, m => m.ApplyId).Select(x => x.ToPOCO());
            int rowCount = OperateContext.Current.BLLSession.Iqgzx_zm_applyBLL.GetListBy(x => x.ApplyId != null).Count();

            //**************方法1、实现 多表显示在一个视图*****
            List<CourseStuModel> resList = new List<CourseStuModel>();
            foreach(qgzx_zm_apply model in list)
            {
                CourseStuModel Comodel = new CourseStuModel();
                qgzx_student_information stuModel = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetListBy(x => x.StudentId == model.StudentId, m => m.StudentId).FirstOrDefault();
                Comodel.ApplyId = model.ApplyId;
                Comodel.ApplyTime = model.ApplyTime;
                Comodel.FirstCourseCode = model.FirstCourseCode;
                Comodel.SecondCourseCode = model.SecondCourseCode;
                Comodel.FirstCourseName = model.FirstCourseName;
                Comodel.SecondCourseName = model.SecondCourseName;
                Comodel.Period = model.Period;
                Comodel.IsChosed = model.IsChosed;
                Comodel.Times = model.Times;
                Comodel.StudentName = stuModel.StudentName;
                Comodel.StudentNum = stuModel.StudentNum;
                resList.Add(Comodel);
            }
            //******************end方法1************************

            #region -------------法2----实现一个视图显示两个表的数据
            //*********法2----实现一个视图显示两个表的数据*************以下做的是先用datatable把数据封装起来，然后再转为泛型**********
            //DataTable dt = new DataTable();
            //DataColumn col = new DataColumn("ApplyId");//
            //DataColumn col1 = new DataColumn("ApplyTime");
            //DataColumn col2 = new DataColumn("FirstCourseId");
            //DataColumn col3 = new DataColumn("SecondCourseId");
            //DataColumn col4 = new DataColumn("IsChosed");
            //DataColumn col5 = new DataColumn("PeriodId");
            //DataColumn col6 = new DataColumn("StudentName");
            //DataColumn col7 = new DataColumn("StudentNum");
            //dt.Columns.Add(col);
            //dt.Columns.Add(col1);
            //dt.Columns.Add(col2);
            //dt.Columns.Add(col3);
            //dt.Columns.Add(col4);
            //dt.Columns.Add(col5);
            //dt.Columns.Add(col6);
            //dt.Columns.Add(col7);

            //foreach(qgzx_zm_apply model in list)
            //{        
            //    qgzx_student_information stuModel = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetListBy(x => x.StudentId == model.StudentId, m => m.StudentId).FirstOrDefault();               
            //DataRow dr = dt.NewRow();
            //dr[0] = model.ApplyId.ToString();
            //dr[1] = model.ApplyTime.ToString();
            //dr[2] = model.FirstCourseId.ToString();
            //dr[3] = model.SecondCourseId.ToString();
            //dr[4] = model.IsChosed.ToString();
            //dr[5] = model.PeriodId.ToString();
            //dr[6] = stuModel.StudentName.ToString();
            //dr[7] = stuModel.StudentNum.ToString();
            //dt.Rows.Add(dr);
            //}

            //List<CourseStuModel> resList = new List<CourseStuModel>();
            //for (int j = 0; j < dt.Rows.Count; j++)
            //{
            //    CourseStuModel model = new CourseStuModel();
            //    PropertyInfo[] infos = model.GetType().GetProperties();
            //    for (int i = 0; i < dt.Columns.Count; i++)
            //    {
            //        foreach (var item in infos)
            //        {
            //            if (dt.Columns[i].ColumnName.Equals(item.Name))
            //            {
            //                if (item.PropertyType == typeof(String))
            //                {
            //                    item.SetValue(model, dt.Rows[j][i] == null ? "" : dt.Rows[j][i].ToString(), null);
            //                }
            //                else if (item.PropertyType == typeof(Int32))
            //                {
            //                    item.SetValue(model, dt.Rows[j][i] == null || dt.Rows[j][i].ToString().Equals("") ? 0 : Convert.ToInt32(dt.Rows[j][i].ToString()), null);
            //                }
            //                else if (item.PropertyType ==typeof(System.DateTime))
            //                {
            //                    item.SetValue(model, dt.Rows[j][i] == null || dt.Rows[j][i].Equals("") ? "" : dt.Rows[j][i], null);
            //                }

            //            }
            //        }
            //    }
            //    resList.Add(model);
            //}

            //**********************测试代码*******************88 
            #endregion
           
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = resList,
                footer = null
            };
            return Json(dgMode);
        } 
        #endregion

        //public ActionResult  GetPeriodData()
        //{
        //    var list = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.GetListBy(x => x.Id != null, x => x.Id);
        //    int rowCount = list.Count();
        //    MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
        //    {
        //        total = rowCount,
        //        rows = list,
        //        footer = null
        //    };
        //    return Json(dgMode);
        //}

        #region 加载修改竹铭课程 window 的html + editZmApply()
        /// <summary>
        /// 加载修改竹铭期数开启 window 的html
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult editZmApply(int id)
        {
            SetDropDonwList();
            //根据id 查询要修改的权限
            var model = OperateContext.Current.BLLSession.Iqgzx_zm_applyBLL.GetListBy(x => x.ApplyId == id).FirstOrDefault().ToViewModel();
            //将权限对象 传给视图的model属性
            return PartialView(model);
        }
        #endregion

        #region post修改课程信息 editZmApply(MODEL.qgzx_zm_apply model)
        /// <summary>
        ///post修改期数信息 editZmApply(MODEL.qgzx_zm_apply model)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult editZmApply(MODEL.qgzx_zm_apply model)
        {
            int res = OperateContext.Current.BLLSession.Iqgzx_zm_applyBLL.Modify(model, "ApplyId", "Period", "StudentId", "FirstCourseCode", "SecondCourseCode", "FirstCourseName", "SecondCourseName", "ApplyTime", "IsChosed","Times");
            if (res > 0)
            {
                return Redirect("/Zmjh/ZmApply/index");
            }
            return Redirect("/Zmjh/ZmApply/index");
        }
        #endregion

        #region 删除课程 + DelAppStudent()
        /// <summary>
        /// 删除课程 + DelCourse()
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult DelApplyStu()
        {
            try
            {
                int id = int.Parse(Request.Form["id"]);
                OperateContext.Current.BLLSession.Iqgzx_zm_applyBLL.DelBy(x => x.ApplyId == id);
                return OperateContext.Current.RedirectAjax("ok", "删除成功~", null, "");
            }
            catch (Exception ex)
            {

                return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被使用,请先取消角色中的这个权限" + ex, null, "");
            }
        }
        #endregion
        
        #region 在这新增课程 html代码 + AddApplyStu()
        /// <summary>
        /// 在这新增新一期竹铭并且开启报名系统  html代码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxRequest]
        public ActionResult AddApplyStu()
        {

            SetDropDonwList();
            return PartialView("EditZmApply");
        }
        #endregion

        #region 在这新增课程 html代码 + AddApplyStu(MODEL.qgzx_zm_apply model)
        /// <summary>
        /// 在这新增期数 html代码
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxRequest]
        public ActionResult AddApplyStu(MODEL.qgzx_zm_apply model)
        {
            qgzx_zm_period mode = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.GetListBy(x => x.IsOpen == 1).FirstOrDefault();
            model.Period = mode.Period;
            model.IsChosed = 0;
            model.ApplyTime = System.DateTime.Now;
            int res = OperateContext.Current.BLLSession.Iqgzx_zm_applyBLL.Add(model);

            if (res > 0)
            {
                return Redirect("/Zmjh/ZmApply/index?ok");
            }
            return Redirect("/Zmjh/ZmApply/index?err");
        }
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult Search()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Search()
        {
            string Period = Request.QueryString["Period"];
            string IsChosed = Request.QueryString["IsChosed"];

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_zm_applyBLL.GetListBy(x => x.ApplyId != null).ToList();
            if (Period != "")
            {
                list = list.Where(x => x.Period.ToString().Contains(Period)).ToList();
            }
            if (IsChosed != "")
            {
                list = list.Where(x => x.IsChosed.ToString().Contains(IsChosed)).ToList();
            }

            int rowCount = list.Count();
            //return OperateContext.Current.RedirectAjax("ok", "加载成功", list, "");
            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = list,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 高级搜索(比较笨，先用这个方法) + ActionResult CourseSearch()
        /// <summary>
        /// 高级搜索(比较笨，先用这个方法)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CourseSearch()
        {

            string CourseName = Request.QueryString["CourseName"];//数据筛选的参数

            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求页码
            int pageIndex = int.Parse(Request.Form["page"]);
            //高级搜索的功能！！！
            var list = OperateContext.Current.BLLSession.Iqgzx_zm_applyBLL.GetListBy(x => x.ApplyId != null).ToList();

            if (CourseName != "")
            {
                list = list.Where(x => x.FirstCourseName.Contains(CourseName)).ToList();
            } 
            else if (CourseName =="" )
            {
                list = list.Where(x => x.FirstCourseName.Contains(CourseName)).ToList();
            }
             
            else
            {
                return OperateContext.Current.RedirectAjax("err", "发生错误，请联系管理员解决", null, "");
            }
            int rowCount = list.Count();

            //**************方法1、实现 多表显示在一个视图*****
            List<CourseStuModel> resList = new List<CourseStuModel>();
            foreach (qgzx_zm_apply model in list)
            {
                CourseStuModel Comodel = new CourseStuModel();
                qgzx_student_information stuModel = OperateContext.Current.BLLSession.Iqgzx_student_informationBLL.GetListBy(x => x.StudentId == model.StudentId, m => m.StudentId).FirstOrDefault();
                Comodel.ApplyId = model.ApplyId;
                Comodel.ApplyTime = model.ApplyTime;
                Comodel.FirstCourseCode = model.FirstCourseCode;
                Comodel.SecondCourseCode = model.SecondCourseCode;
                Comodel.FirstCourseName = model.FirstCourseName;
                Comodel.SecondCourseName = model.SecondCourseName;
                Comodel.Period = model.Period;
                Comodel.IsChosed = model.IsChosed;
                Comodel.Times = model.Times;
                Comodel.StudentName = stuModel.StudentName;
                Comodel.StudentNum = stuModel.StudentNum;
                resList.Add(Comodel);
            }
            //******************end方法1************************

            MODEL.EasyUIModel.DataGridModel dgMode = new MODEL.EasyUIModel.DataGridModel()
            {
                total = rowCount,
                rows = resList,
                footer = null
            };
            return Json(dgMode);
        }
        #endregion

        #region 设置新增和修改通用的下拉框数据 + SetDropDonwList()
        /// <summary>
        /// 设置新增和修改通用的下拉框数据
        /// </summary>
        public void SetDropDonwList()
        {
            /*
             课程id对应课程
             */
            List<MODEL.qgzx_zm_course> list = OperateContext.Current.BLLSession.Iqgzx_zm_courseBLL.GetListBy(x => x.Id != null).Where(x => x.IsOpen == 1).ToList();
            List<SelectListItem> selectList = new List<SelectListItem>();
            foreach (MODEL.qgzx_zm_course model in list)
            {
                selectList.Add(new SelectListItem() { Text = model.CourseName, Value = model.CourseName });
            }
            ViewBag.courselist = selectList;          
        }
        #endregion

        //public void SetPeriodDropDownList()
        //{
        //    List<MODEL.qgzx_zm_period> list = OperateContext.Current.BLLSession.Iqgzx_zm_periodBLL.GetListBy(x => x.Id != null).ToList();
        //    List<SelectListItem> selectList = new List<SelectListItem>();
        //    foreach (MODEL.qgzx_zm_period model in list)
        //    {
        //        selectList.Add(new SelectListItem() { Text = model.Period, Value = model.Period.ToString() });
        //    }
        //    ViewBag.PeriodList = selectList;   
        //}


    }
}
